package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 读者界面操作
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }
    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user){
        try{
            userService.add(user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增用户失败");
        }
        return new Result(true,"新增用户成功");
    }
    //删除用户
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            userService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
        return new Result(true,"删除用户成功");
    }
    //编辑用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user){
        try{
            userService.edit(user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑用户失败");
        }
        return new Result(true,"编辑用户成功");
    }
    //新增用户
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            User user = userService.findById(id);
            return new Result(true,"查询用户成功",user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询用户失败");
        }
    }
}
